package ljx;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

public class index5 extends HttpServlet {
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        // 设置响应格式
        response.setContentType("text/jsp");
        // 接收请求中的参数
        String name = request.getParameter("name");
        String userid= request.getParameter("userid");
        String b_target = request.getParameter("b_target");
        String time = request.getParameter("time");

        boolean isTrue = false;
        Connection conn = null;
        PreparedStatement ps = null;
        try {
            conn = JdbcUtils.getConnection();
            String sql = "insert into index1(name,userid,b_target,time) values(?,?,?,?)";
            System.out.println("=========注册==========");
            ps = conn.prepareStatement(sql);
            ps.setString(1, name+ "");
            ps.setString(2, userid+ "");
            ps.setString(3, b_target+"");
            ps.setString(4, time+"");

            // 用int接收数据
            int count = ps.executeUpdate();
            if (count > 0) {
                // 注册成功
                isTrue = true;
                System.out.println("注册成功！");
                response.sendRedirect("index5.jsp");

            } else {
                isTrue = false;
                System.out.println("失败");
                response.sendRedirect("error.jsp");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        JdbcUtils.relesse(conn, ps,null);
        // 发送响应
        PrintWriter out = response.getWriter();
        out.flush();
        out.close();


    }

    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req, resp);
    }
}
